package com.sxt.sys.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.constast.SysConstast;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.domain.Role;
import com.sxt.sys.service.IPermissionService;
import com.sxt.sys.service.IRoleService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.vo.RoleVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2019-08-12
 */
@RestController
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IPermissionService permissionService;
	
	@RequestMapping("loadAllRole")
	public DataGridView loadAllRole(RoleVo roleVo) {
		IPage<Role> page = new Page<>(roleVo.getPage(),roleVo.getLimit());
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(roleVo.getName())) {
			queryWrapper.like("name", roleVo.getName());
		}
		if(StringUtils.isNotBlank(roleVo.getRemark())) {
			queryWrapper.like("remark", roleVo.getRemark());
		}
		if(roleVo.getAvailable()!=null) {
			queryWrapper.eq("available", roleVo.getAvailable());
		}
		this.roleService.page(page,queryWrapper);
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	
	@RequestMapping("addRole")
	public ResultObj addRole(RoleVo roleVo) {
		try {
			roleVo.setCreatetime(new Date());
			this.roleService.save(roleVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}
	
	
	@RequestMapping("updateRole")
	public ResultObj updateRole(RoleVo roleVo) {
		try {
			this.roleService.updateById(roleVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
			
		}
	}
	
	
	@RequestMapping("deleteRole")
	public ResultObj deleteRole(Integer id) {
		try {
			this.roleService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	@RequestMapping("initRolePermissionTreeJson")
	public DataGridView initRolePermissionTreeJson(Integer id) {
		QueryWrapper<Permission> queryWrapper1 = new QueryWrapper<Permission>();
		queryWrapper1.eq("available",SysConstast.AVAILABLE_TRUE);
		List<Permission> allPermission = this.permissionService.list(queryWrapper1);
		List<Integer> permissionIds =this.roleService.queryRolePermissionIdsByRId(id);
		List<Permission> currentRolePermissions=null;
		if(permissionIds!=null&&permissionIds.size()>0) {
			QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<Permission>();
			queryWrapper2.eq("available", SysConstast.AVAILABLE_TRUE);
			queryWrapper2.in("id", permissionIds);
			currentRolePermissions=this.permissionService.list(queryWrapper2);
		}else {
			currentRolePermissions= new ArrayList<>();
		}
		List<TreeNode> nodes = new ArrayList<>();
		for (Permission p1 : allPermission) {
			String checkArr="0";
			for (Permission p2 : currentRolePermissions) {
				if(p1.getId()==p2.getId()) {
					checkArr="1";
					break;
				}
			}
			Boolean spread=p1.getOpen()==SysConstast.SPREAD_TRUE?true:false;
			nodes.add(new TreeNode(p1.getId(), p1.getPid(), p1.getTitle(), spread, checkArr));
		}
		return new DataGridView(nodes);
	}
	
	@RequestMapping("saveRolePermission")
	public ResultObj saveRolePermission(RoleVo roleVo) {
		try {
			this.roleService.saveRolePermission(roleVo.getId(),roleVo.getIds());
			return ResultObj.DISPATCH_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DISPATCH_ERROR;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
